//
//  CAVideoCell.m
//  cascade-realm
//
//  Created by QSC on 1/6/24.
//

#import "CAVideoCell.h"
@interface CAVideoCell()
@property (nonatomic,strong)UIView * bottomView;
@property (nonatomic,strong)UIImageView * iconImgv;
@property (nonatomic,strong)UIImageView * addImgv;
@property (nonatomic,strong)UIImageView * bgImgV;
@property (nonatomic,strong)UILabel * nameLabel;
@property (nonatomic,strong)UILabel * contentLabel;
@property (nonatomic,strong)UIButton* likeBtn;
@end
@implementation CAVideoCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

- (void)setUp{
    
    UIView *contentView = self.contentView;
    
    UILabel * videoLabel = [UILabel LabelWithFont:BoldFont(22) color:[UIColor whiteColor] text:@"video"];
    
    [contentView addSubview:videoLabel];
    [videoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView).offset(kNavigationBarHeight + 10);
        make.left.equalTo(contentView).offset(27);
    }];
    
    UIImageView * videoImg = [[UIImageView alloc] initWithImage:Img(@"icon_home")];
    [contentView insertSubview:videoImg belowSubview:videoLabel];
    [videoImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(videoLabel);
        make.left.equalTo(videoLabel.mas_right).offset(-15);
        make.size.mas_equalTo(30);
    }];
    
    UIButton * moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreBtn setImage:Img(@"video_more") forState:UIControlStateNormal];
    [contentView addSubview:moreBtn];
    [moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(videoLabel);
        make.right.equalTo(contentView).offset(-20);
        make.size.mas_equalTo(28);
    }];
    [moreBtn addTarget:self action:@selector(moreClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView * bgImageV = [UIImageView new];
    bgImageV.contentMode = UIViewContentModeScaleAspectFill;
    bgImageV.backgroundColor = [UIColor lightGrayColor];
    [contentView addSubview:bgImageV];
    [contentView insertSubview:bgImageV atIndex:0];
    self.bgImgV = bgImageV;
    

    UIView * bottomView = [UIView new];
    bottomView.backgroundColor = Hex(0x383E12);
    [contentView addSubview:bottomView];

    self.bottomView = bottomView;
    
    
    CGFloat imgH = IsX?ScreenW/375 * 550:ScreenW/375 * 400;
    [bgImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(contentView);
        make.height.mas_equalTo(imgH);
    }];
    
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(contentView);
        make.top.equalTo(bgImageV.mas_bottom);
    }];
    
    
    [self setUpBottomView];
    
}


- (void)setUpBottomView{
    UIImageView * iconImgV = [UIImageView new];
    UIView * bottomView = self.bottomView;
    iconImgV.layer.borderWidth = 1;
    iconImgV.layer.borderColor  = [UIColor whiteColor].CGColor;
    iconImgV.layer.cornerRadius = 16;
    iconImgV.clipsToBounds = YES;
    [bottomView addSubview:iconImgV];
    [iconImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bottomView).offset(20);
        make.size.mas_equalTo(32);
        make.top.equalTo(bottomView).offset(100);
    }];
    self.iconImgv = iconImgV;
    iconImgV.userInteractionEnabled = YES;
    WeakSelf
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [Tool Follow:weakSelf.user.userId];
    }];
    [iconImgV addGestureRecognizer:tap];
    
    UIImageView *addimg = [[UIImageView alloc] initWithImage:Img(@"add_home")];
    [bottomView addSubview:addimg];
    [addimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(iconImgV);
        make.bottom.equalTo(iconImgV).offset(-2);
        make.size.mas_equalTo(12);
    }];
    self.addImgv = addimg;
    
    UILabel * nameLabel = [UILabel LabelWithFont:Font(14) color:[UIColor colorWithWhite:1.0 alpha:0.8] text:@""];
    self.nameLabel = nameLabel;
    [bottomView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(iconImgV);
        make.left.equalTo(iconImgV.mas_right).offset(12);
        make.right.equalTo(bottomView).offset(-60);
    }];
    
    UILabel * contentLabel = [UILabel LabelWithFont:BoldFont(18) color:[UIColor whiteColor] text:@""];
    self.contentLabel = contentLabel;
    contentLabel.numberOfLines = 0;
    [bottomView addSubview:contentLabel];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconImgV.mas_bottom).offset(20);
        make.left.equalTo(iconImgV);
        make.right.equalTo(bottomView).offset(-20);
    }];
    
    
    UIButton * likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [likeBtn setBackgroundImage:Img(@"video_unlike") forState:UIControlStateNormal];
    [likeBtn setBackgroundImage:Img(@"video_like") forState:UIControlStateSelected];
    self.likeBtn = likeBtn;
    [bottomView addSubview:likeBtn];
    [likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(iconImgV);
        make.right.equalTo(bottomView).offset(-25);
        make.size.mas_equalTo(25);
    }];
    [likeBtn addTarget:self action:@selector(likeClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton * playBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    [playBtn setBackgroundImage:Img(@"play") forState:UIControlStateNormal];
    [bottomView addSubview:playBtn];
    [playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(60);
        make.right.equalTo(bottomView).offset(-24);
        make.top.equalTo(bottomView).offset(22);
    }];
    
    [playBtn addTarget:self action:@selector(playClick) forControlEvents:UIControlEventTouchUpInside];
}


- (void)setUser:(CAHomeUser *)user{
    _user = user;
    self.nameLabel.text = user.nickname;
    self.contentLabel.text = user.viodeTitle;
    [self.iconImgv sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:Img(@"defult_head")];
    self.likeBtn.selected = user.isVideolike;
  
//    NSURL * url = [NSURL URLWithString:user.viodeUrl];
//    WeakSelf
//    [VideoAlertView getFristFramFormUrl:url sucess:^(UIImage * _Nonnull img) {
//        weakSelf.bgImgV.image = img;
//    }];
    
    [self.bgImgV sd_setImageWithURL:[NSURL URLWithString:user.viodeCover]];
    
    self.addImgv.hidden = [CAUserInfo.shared.followIds containsObject:user.userId];
}


- (void)moreClick{
    [Tool moreClick:self.user.userId];
}

- (void)likeClick{
    
    [Tool ShowLoading];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [Tool HideLoading];
       
        
        [self.user SetIsVideolikeInDB:!self.user.isVideolike];
      
        self.likeBtn.selected = self.user.isVideolike;
   
    });
}


- (void)playClick{

    NSURL * url = [NSURL URLWithString:self.user.viodeUrl];
    [VideoAlertView ShowAndPlayUrl:url];
}




@end
